package com.hyst.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hyst.domain.*;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hyst.common.Contants;
import com.hyst.common.ResultBean;
import com.hyst.common.ResultPage;
import com.hyst.service.LeaveRequestService;
import com.hyst.utils.DateUtil;
import com.mysql.fabric.xmlrpc.base.Array;

/**
 * @Description:controller
 * @author：bin 
 *@Date：2018年10月07日
 */
@RestController
@RequestMapping("leaveRequests")
@CrossOrigin(maxAge = 3600, origins = "*")
public class LeaveRequestController {
	
	@Autowired
	private LeaveRequestService leaveRequestService;
	
	/**
	 * 获取历史请假列表
	 * @param leaveRequest
	 * @return
	 */
	@GetMapping("/list")
	public ResultPage<LeaveRequest> list(LeaveRequest leaveRequest,HttpServletRequest request,HttpServletResponse response) {
		Admin admin = getLoginInfoFromSession( request, response);
		if(admin==null){
			return ResultPage.fail("没有登录信息",ResultBean.FAIL);
		}
		try {
			leaveRequest.setAdminId(admin.getUniqueid());
			return ResultPage.success(leaveRequestService.list(leaveRequest));
		} catch (Exception e) {
		    e.printStackTrace();
			return ResultPage.fail("获取请假列表发生异常", ResultBean.FAIL);
		}
	}

	@GetMapping("/list2")
	public ResultPage<LeaveRequest> list2(LeaveRequest leaveRequest,HttpServletRequest request,HttpServletResponse response) {
		Admin admin = getLoginInfoFromSession( request, response);
		if(admin==null){
			return ResultPage.fail("没有登录信息",ResultBean.FAIL);
		}
		leaveRequest.setAdminId(admin.getUniqueid());
		return ResultPage.success(leaveRequestService.list2(leaveRequest));
	}


	@GetMapping("/students")
	public ResultPage<StudentLeaveRequest> studentsLeval(LeaveRequest leaveRequest, HttpServletRequest request, HttpServletResponse response) {
		Admin admin = getLoginInfoFromSession( request, response);
		if(admin==null){
			return ResultPage.fail("没有登录信息",ResultBean.FAIL);
		}
		leaveRequest.setAdminId(admin.getUniqueid());
		return ResultPage.success(leaveRequestService.studentLeave(leaveRequest));
	}


	/**
	 * 获取可以请假日期范围内的课程日历
	 * @param leaveRequest
	 * @return
	 */
	@GetMapping("/getCurriculumsByDays")
	public ResultPage<CurriculumScheduleView> getCurriculumsByDays(final LeaveRequest leaveRequest,HttpServletRequest request,HttpServletResponse response) {
		Admin admin = getLoginInfoFromSession( request, response);
		if(admin==null){
			return ResultPage.fail("没有登录信息",1);
		}
		String leaveId = leaveRequest.getUniqueId();
		if(StringUtils.isBlank(leaveId)){
			return ResultPage.fail("请求信息为空",1);
		}
		try {
			LeaveRequest leaveInfo = leaveRequestService.getLeaveInfoById(leaveId);
			if(leaveInfo==null){
				return ResultPage.fail("请假信息不存在",1);
			}
			String leaveDays = leaveInfo.getLeaveDateStr();
			String teacherId = leaveInfo.getTeacherId();
			List<String> leaveDayList = new ArrayList<String>();
			if(leaveDays.indexOf(",")>0){
				String[] dayArr = leaveDays.split(",");
				for(String day:dayArr){
					leaveDayList.add(day);
				}
			}else{
				leaveDayList.add(leaveDays);
			}
			return ResultPage.success(leaveRequestService.getCurriculumsByDays(teacherId,leaveDayList));
		} catch (Throwable e) {
			return ResultPage.fail("获取课程信息发生异常", ResultBean.FAIL);
		}
	}
	

	/**
	 * @Description: 请假审核
	 * @param leaveRequest
	 * @return ResultBean<LeaveRequest>
	 */
	@GetMapping("/updateLeaveRequestStatus")
	public ResultBean<String> updateLeaveRequestStatus(final LeaveRequest leaveRequest) {
		try {
			int result = leaveRequestService.updateLeaveRequestStatus(leaveRequest);
			if(result>0){
				return ResultBean.success("修改成功",ResultBean.SUCCESS);
			}else{
				return ResultBean.fail("修改失败");
			}
		} catch (Throwable e) {
			return ResultBean.fail("审核异常");
		}
	}

	/**
	 * @Description: 分配负责人
	 * @param leaveRequest
	 * @return ResultBean<LeaveRequest>
	 */
	@GetMapping("/updateAdminForLeave")
	public ResultBean<String> updateAdminForLeave(final LeaveRequest leaveRequest) {
		try {
			int result = leaveRequestService.updateAdminForLeave(leaveRequest);
			if(result>0){
				return ResultBean.success("分配成功",ResultBean.SUCCESS);
			}else{
				return ResultBean.fail("分配失败");
			}
		} catch (Throwable e) {
			return ResultBean.fail("分配异常");
		}
	}

	@GetMapping("updateStatus")
	public ResultBean<Object> updateStatus(@RequestParam("teacherId") String teacherId,
								   @RequestParam("status")Integer status,
								   @RequestParam("submissionTime") String submissionTime,
								   HttpServletRequest request,
								   HttpServletResponse response){
		try {
			Admin admin = getLoginInfoFromSession( request, response);
			if(admin==null){
				return ResultBean.fail("没有登录信息");
			}
			Map<String,Object> sqlMap = new HashMap<>();
			sqlMap.put("state",status);
			sqlMap.put("teacherId",teacherId);
			sqlMap.put("adminId",admin.getUniqueid());
			sqlMap.put("submissionTime",submissionTime);
			leaveRequestService.updateStatus(sqlMap);
		}catch (Exception e){
			e.printStackTrace();
		}
		return ResultBean.success(null);
	}


	private Admin getLoginInfoFromSession(HttpServletRequest request,HttpServletResponse response){
		if(request==null || request.getSession()==null){
			return null;
		}
		Admin admin = (Admin) request.getSession().getAttribute(Contants.SESSION_USER_INFO);
		if(admin==null){
			return null;
		}
		return admin;
	}
}
